<?php

namespace App\Admin\Controllers;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Encore\Admin\Layout\Content;
use Encore\Admin\Controllers\RoleController as BaseRoleController;

class RoleController extends BaseRoleController
{


    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $roleModel = config('admin.database.roles_model');

        $grid = new Grid(new $roleModel());

        $backend = request('backend','');
        if($backend) $grid->model()->where('backend',$backend);

        $grid->column('id', 'ID')->sortable();
        $grid->column('slug', trans('admin.slug'));
        $grid->column('name', trans('admin.name'));
		$grid->column('backend','后台')->label('info');

        $grid->column('permissions', trans('admin.permission'))->pluck('name')->label();

        //$grid->column('created_at', trans('admin.created_at'));
        //$grid->column('updated_at', trans('admin.updated_at'));

        $grid->actions(function (Grid\Displayers\Actions $actions) {
            if ($actions->row->slug == 'administrator') {
                $actions->disableDelete();
            }
        });

        $grid->tools(function (Grid\Tools $tools) {
            $tools->batch(function (Grid\Tools\BatchActions $actions) {
                $actions->disableDelete();
            });
        });
		$grid->header(function () use($backend){
            $backends = get_backends();
            $sel='';
            foreach($backends as $v){
                    $selected =  ($v==$backend) ? "selected" : "";
                    $sel .= "<option value='{$v}' {$selected}>{$v}</option>";
            }
            $str ='<div class="row">
                    <div class="col-md-6" style="margin-left:30px;">
                        <div class="input-group input-group-sm">
                            <div class="input-group-addon btn btn-primary"><i class="fa fa-list"></i> 后台选择</div>
                            <select name="backend" onchange="window.location.href=\'?backend=\'+this.options[this.options.selectedIndex].value"  class="form-control">
                            <option value="">全部</option>'.$sel.'
                            </select>
                        </div>
                    </div>
                    <div class="col-md-6">
                    </div>
                </div>';
            return $str;
        });
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        $roleModel = config('admin.database.roles_model');

        $show = new Show($roleModel::findOrFail($id));

        $show->field('id', 'ID');
		$show->field('backend', '后台');
        $show->field('slug', trans('admin.slug'));
        $show->field('name', trans('admin.name'));
        $show->field('permissions', trans('admin.permissions'))->as(function ($permission) {
            return $permission->pluck('name');
        })->label();
        //$show->field('created_at', trans('admin.created_at'));
        //$show->field('updated_at', trans('admin.updated_at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    public function form()
    {
        $permissionModel = config('admin.database.permissions_model');
        $roleModel = config('admin.database.roles_model');

        $form = new Form(new $roleModel());

        $form->display('id', 'ID');

        $form->text('slug', trans('admin.slug'))->rules('required');
        $form->text('name', trans('admin.name'))->rules('required');
        $form->display('backend','后台');
        $form->listbox('permissions', trans('admin.permissions'))->options(function() use($permissionModel){
				if(!empty($this->id)){
					return $permissionModel::whereRaw('FIND_IN_SET(?,backend)',[$this->backend])->pluck('name', 'id');
				}
			});

        //$form->display('created_at', trans('admin.created_at'));
        //$form->display('updated_at', trans('admin.updated_at'));

        return $form;
    }

    public function create(Content $content)
    {
        return $content
            ->title($this->title())
            ->description($this->description['create'] ?? trans('admin.create'))
            ->body($this->newform());
    }

	public function newform()
	{
        $roleModel = config('admin.database.roles_model');

        $form = new Form(new $roleModel());

        $form->display('id', 'ID');
        $form->text('slug', trans('admin.slug'))->rules('required');
        $form->text('name', trans('admin.name'))->rules('required');
        $form->select('backend','后台')->options(function(){
					$backend = get_backends();
					$backend = array_combine($backend,$backend);
                    return $backend;
                })->required();

        //$form->display('created_at', trans('admin.created_at'));
        //$form->display('updated_at', trans('admin.updated_at'));

        return $form;
    }
}
